java

您所在的位置:网站首页 drools map操作 java

java

2023-07-28 11:17| 来源: 网络整理| 查看: 265

我正在编写一个 RESTful 服务来根据某些规则计算某些值。

例如:

有一个这样的 JSON:

{ "amount": 100, "destination":"A" }

此数据是请求正文,发布到我的 Controller :

@RequestMapping(value = "/orders", method= RequestMethod.POST) public void getOrderRequest(@RequestBody Order order){ // use Drools to calculate and return the result }

这是实体类:

public class Order{ private Integer amount; private String destination; private Float price; // getters and setters }

我使用Drools计算价格(假代码):

package rules import entity.Order rule "rule1" no-loop true lock-on-active true salience 1 when $s : Order(amount 50 && destination=="A") then $s.setPrice(2000); update($s);

这工作正常。所有规则都可以更改并运行,而无需重新启动我的 REST 服务。

这是一个场景,一个新字段添加到订单 JSON 中:

{ "amount": 100, "destination":"A", "customer":"Trump" }

添加新规则: 如果客户是特朗普,则价格加倍:

rule "rule3" no-loop true lock-on-active true salience 1 when $s : Order(customer == "Trump") then $s.setPrice(price * 2); update($s);

但是我必须向我的 Order 类添加一个新变量并重新启动我的服务。

我想知道有什么方法可以在不重新启动我的 REST 服务的情况下实现它吗?

或者我可以使用其他 BRMS 动态处理 JSON 数据吗?

最佳答案

使用Map来携带所有订单详细信息:

public class Order{ private Map fields; // getters and setters }

如果您设法将 JSON 中的值添加到 Map 中,那么您可以像这样编写规则:

rule "rule3" no-loop true lock-on-active true salience 1 when $s : Order(fields["customer"] == "Trump") then $s.setPrice(price * 2); update($s);

关于java - 如何在 Drools 中处理动态 Json/类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53495152/



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3